package org.example.dao;

import org.example.model.Account;
import org.example.util.JDBCUtils;

import java.sql.*;

public class register {
    // 判断账户是否被占用
    public static boolean judge(Account account){
        Connection connection = null;
        Statement statement=null;
        ResultSet resultSet=null;
        try {
            connection = JDBCUtils.getConnection();
            statement= connection.createStatement();
            resultSet= statement.executeQuery("select name from account");
            while (resultSet.next()){
                if(resultSet.getString("name").equals(account.getName())){
                    System.out.println("用户名已存在，请重新输入");
                    return false;
                }

            }
            return true;
        } catch (SQLException e) {
            System.out.println("检查出错"+e.getMessage());
            return false;
        }
    }
    // 注册账户
    public static boolean regist(Account account){
        Connection connection=null;
        PreparedStatement statement=null;
        try {
            connection= JDBCUtils.getConnection();
             statement= connection.prepareStatement("insert into account values (6,?,?)");
             statement.setString(1,account.getName());
             statement.setString(2,account.getPassword());
            int i = statement.executeUpdate();
            return  i>0;
        } catch (SQLException e) {
            System.out.println("注册失败");
            return false;
        }

    }
}
